#! /usr/bin/python
# -*- coding: cp1250 -*-

import string
import re
import pepe.p_txt.bigtxt as bt

class ColumnsSpaces:
    """ find empty columns (containing only spaces)

    NOTE : last line in data file should be empty, if not try it and notice last line, last column 
    (there will be one more last empty column in this last line) ... no time to fix this now
    """
    def __init__(self):
        """ """
        self.spaces = ['_']
        self.rowno = 0

    def examine(self, f):
        """ """
        self.file = f
        for line in bt.lines(f):
            #line = re.sub('\n', '', line)
            self.rowno += 1
            if self.spaces == ['_']: self.spaces = range(0, len(line))
            tmpSpaces = self.spaces[:]
            for indx in tmpSpaces:
                if indx < len(line):
                    if line[indx] <> ' ': self.spaces.remove(indx)
        #print '>> rows examined >> ', self.rowno
        tmpSpaces = self.spaces[:]
        for indx in tmpSpaces:
            if (indx + 1) in self.spaces:
                self.spaces.remove(indx)
        if self.spaces[0] <> 0: self.spaces.insert(0, 0)

def columnsCutter(data, cIndx):
    """ cut data to columns
    """
    res = []
    if len(data) not in (cIndx[-1], (cIndx[-2] + 1)): cIndx.append(len(data))
    for i,v in enumerate(cIndx):
        #print i,v
        if v <> 0:
            v0 = cIndx[i-1]
            v1 = v
            res.append(string.strip(data[v0:v1]))
    return res

if __name__ == '__main__':
    ''' '''
    import pepe.p_txt.bigtxt as bt
    import pepe.p_txt.txt_columns as tc
    import re

    c = tc.ColumnsSpaces()
    c.examine('example_txt_columns.txt')
    print c.spaces

    '''
    for L in bt.lines('example_txt_columns.txt'):
        print tc.columnsCutter(L, c.spaces)
    '''
    
    for L in bt.lines('example_txt_columns.txt'):
        coln = 0
        print L
        for col in tc.columnsCutter(L, c.spaces):
            coln += 1
            print coln, col
